package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("要添加的数据:{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功!");
    }


    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    private R<Page> list(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, lqw);
        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category c = categoryService.getById(categoryId);
            if (c != null) {
                String categoryName = c.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    @DeleteMapping
    public R<String> delete(Long[] ids) {
        log.info("要删除套餐的ids{}", Arrays.toString(ids));
        //根据id查询数据
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            //只要有销售的立马返回不可以删除
            if (setmeal.getStatus() == 1) {
                return R.error("商品正在销售,不可以删除!");
            }
            //删除商品
            setmealService.removeById(id);
            //删除setmeal_dish表的数据
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(lqw);
        }
        return R.success("删除成功!");
    }


    @PostMapping("/status/{statusId}")
    public R<String> status(@PathVariable("statusId") int statusId, @RequestParam("ids") Long[] ids) {
        log.info("要更改的状态{},要更改数据的ids{}", statusId, Arrays.toString(ids));
        for (Long id : ids) {
            //更新数据
            LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper<>();
            luw.eq(Setmeal::getId, id)
                    .set(Setmeal::getStatus, statusId); // 这里假设要更新状态为"停售"
            setmealService.update(luw);
        }
        return R.success("状态已改为" + (statusId == 0 ? "停售" : "销售"));
    }

    @GetMapping("/list")
    private R<List<Setmeal>> list_all(Setmeal setmeal) {
        log.info("要查询的套餐{}", setmeal);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getCreateTime);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
